<?php


namespace App\Service\Cooperative;


use App\Constants\ContextKey;
use App\Constants\Redis\CooperativeCount;
use App\Utils\Mongo;
use App\Utils\Redis;
use Hyperf\Utils\Context;

class StatisticsService extends BaseService
{

    /**
     * @var
     */
    protected $redis;

    public function __construct()
    {
        $this->redis = di(Redis::class);
    }

    /**
     * 获取统计数据
     * @return mixed
     */
    public function statisticsData()
    {
        $userInfo = Context::get(ContextKey::COOPERATIVE_USER);

        //昨日新增用户数
        $data['user_count'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_ADD_USER,$userInfo->id)));
        //昨日新增悬赏主
        $data['employer_count'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_ADD_EMPLOYER,$userInfo->id)));
        //昨日成交佣金
        $data['commission_total'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_COMMISSION,$userInfo->id)));
        //昨日悬赏主充值
        $data['recharge'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_EMPLOYER_RECHARGE,$userInfo->id)));
        //总用户数
        $data['user_total'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_USER_TOTAL,$userInfo->id)));
        //总悬赏主数
        $data['employer_total'] =  intval($this->redis->get(sprintf(CooperativeCount::COOPER_EMPLOYER_TOTAL,$userInfo->id)));

        return $data;
    }

    /**
     * 柱状图数据
     * @param $data
     * @return array
     */
    public function barChartData($data)
    {
        $userInfo = Context::get(ContextKey::COOPERATIVE_USER);
        $start_date = 0;
        $end_date = 0;
        //天数
        if (isset($data['date_days']) && is_numeric($data['date_days'])){
            $days = (int)$data['date_days'] + 1;
            //开始时间日期
            $start_date = strtotime(date('Y-m-d 00:00:00',strtotime('-'.$days.' day')));
            //结束时间日期
            $end_date = strtotime(date('Y-m-d 23:59:59',strtotime('- 1 day')));
        }
        //自定义日期
        if (isset($data['start_date']) && isset($data['end_date'])){
            $start_date = strtotime(date('Y-m-d 00:00:00',strtotime($data['start_date'])));
            $end_date = strtotime(date('Y-m-d 23:59:59',strtotime($data['end_date'])));
        }
        //从mongo获取数据
        $dbKey = config('mongodb.default.db').'.barchart_'.$userInfo->id;
        $where = [
            '$and' => [
                ['date' => ['$gte'=>$start_date]],
                ['date' => ['$lte'=>$end_date]]
            ]
        ];
        $sort = ['sort' => ['created_at' => -1]];
        $result = di(Mongo::class)->query($dbKey,$where,$sort);
        //去重，保留最新的
        $existArr = [];
        $list = [];
        foreach ($result as $key=>$val){
            if (in_array($val->date,$existArr)){
                unset($result[$key]);
            }
            $existArr[] = $val->date;
            $list[$key]['date'] = date('Y-m-d H:i:s',$val->date);
            $list[$key]['count'] = $val->cnt ?? 0;
        }

        return $list;
    }


}